import time

from part1.studentmanager import StudentManager
from part1.studentdo import StudentDO


def main():
    manager = StudentManager.get_instance()
    while 1:
        print("1.根据学号查看学员信息\t\t2.添加学员\t\t3.查看所有学员信息\n")
        func_code = input("请输入编码选择对应功能(输入q退出):")
        if func_code == 'q':
            break
        elif func_code == '1':
            print("===========功能1:学号查看学员信息==========")
            while 1:
                stu_no = input("请输入查询的学号(如 001):")
                single_stu_info = manager.query_student(stu_no)
                format_out_put([single_stu_info])
                if exit_tips("【功能1】"):
                    break
        elif func_code == '2':
            print("===========功能2:添加学员信息==========")
            while 1:
                _no = input("请输入新增学生学号(如 001):")
                _name = input("请输入新增学生姓名(如 张三):")
                _sex = input("请输入新增学生性别(如 男,女):")
                stu_do_info = StudentDO(_no, _name, _sex)
                try:
                    manager.add_student(stu_do_info)
                except Exception as e:
                    print("新增学生信息异常: %s", e)
                if exit_tips("【功能2】"):
                    break
        elif func_code == '3':
            print("===========功能3:查所有学员信息==========")
            while 1:
                single_stu_info = manager.query_all_students()
                format_out_put(single_stu_info)
                if exit_tips("【功能3】"):
                    break
        else:
            print("不支持的功能")


def format_out_put(info_list: list[StudentDO]):
    template = '{0:^10}|{1:^10}|{2:^10}'
    print(template.format('学号', '姓名', '性别'))
    for info in info_list:
        if info is None:
            continue
        else:
            print(template.format(info.get_no, info.get_name, info.get_sex))


def exit_tips(msg: str) -> bool:
    time.sleep(1)
    exit_code = input("\n>>>>>是否需要继续" + msg + "(输入n或q则退出):")
    if 'n' == exit_code or 'q' == exit_code:
        print("\n\n>>>>>你可继续选择其他功能")
        return True
    return False


if __name__ == '__main__':
    main()
